﻿Imports System.Data.SqlClient
Imports System.Data
Imports SuporteModel
Public Class ProvUsuarioDAO
    Implements IUsuario

    Private conection As SqlConnection

    Public Sub New(ByVal connection As SqlConnection)
        Me.conection = connection
    End Sub

    Public Shared Function UsuarioSessao() As Usuario
        Return usuarioSession
    End Function

    Public Shared Sub UsuarioSessao(ByVal parUsuario As Usuario)
        usuarioSession = parUsuario
    End Sub


    Public Function AtualizarUsuario(ByVal parIdUsuario As Integer, ByVal parUsuarioNovo As SuporteModel.Usuario) As Boolean Implements IUsuario.AtualizarUsuario

    End Function

    Public Function BuscaUsuario(ByVal parIdUsuario As Integer) As SuporteModel.Usuario Implements IUsuario.BuscaUsuario
        Dim result As New SuporteModel.Usuario
        Try
            Me.conection.Open()
            Dim comm As New SqlCommand("select * from USUARIOS where us_matricula = @matricula", Me.conection)
            comm.Parameters.AddWithValue("@matricula", parIdUsuario)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()
            If dataReader.Read() Then
                result.Abreviado = dataReader("US_ABREVIADO").ToString()
                result.Acesso = dataReader("US_ACESSO").ToString()
                result.Codigo = dataReader("US_MATRICULA").ToString()
                result.DataCadastro = Convert.ToDateTime(dataReader("US_DATA"))
                result.Inativo = Convert.ToBoolean(dataReader("US_INATIVO"))
                result.Nome = dataReader("US_NOME").ToString()
                result.Senha = dataReader("US_SENHA").ToString()
            End If
        Catch ex As Exception
        Finally
            Me.conection.Close()
        End Try
        Return result
    End Function

    Public Function CadastrarUsuario(ByVal parUsuario As SuporteModel.Usuario) As Boolean Implements IUsuario.CadastrarUsuario

    End Function

    Public Function ExcluirUsuario(ByVal parIdUsuario As Integer) As Boolean Implements IUsuario.ExcluirUsuario

    End Function

    Public Function Login(ByVal parNome As String, ByVal parSenha As String) As SuporteModel.Usuario Implements IUsuario.Login
        Dim fUsuario As New Usuario
        Try
            Me.conection.Open()
            Dim fListaUsuarios As New List(Of Usuario)
            Dim command As New SqlCommand("select * from usuarios where us_abreviado = @abreviado", Me.conection)
            command.Parameters.AddWithValue("@abreviado", parNome)
            Dim dataReader As SqlDataReader = command.ExecuteReader()
            While dataReader.Read()
                Dim sUsuarioWhile As New Usuario
                sUsuarioWhile.Nome = Convert.ToString(dataReader("US_NOME"))
                sUsuarioWhile.Senha = Convert.ToString(dataReader("US_SENHA"))
                sUsuarioWhile.Acesso = Convert.ToString(dataReader("US_SENHA"))
                sUsuarioWhile.Codigo = Convert.ToString(dataReader("US_MATRICULA"))
                sUsuarioWhile.Abreviado = Convert.ToString(dataReader("US_ABREVIADO"))
                If Convert.ToInt64(dataReader("US_INATIVO")) > 0 Then
                    sUsuarioWhile.Inativo = 1
                Else
                    sUsuarioWhile.Inativo = 0
                End If
                If Not IsDBNull(dataReader("US_CADASTRO")) Then
                    sUsuarioWhile.DataCadastro = Convert.ToDateTime(dataReader("US_CADASTRO"))
                End If
                sUsuarioWhile.Email = Convert.ToString(dataReader("US_EMAIL"))
                fListaUsuarios.Add(sUsuarioWhile)
            End While
            For index = 0 To fListaUsuarios.Count - 1
                If UtilDB.Descriptografar(fListaUsuarios.Item(index).Senha) = parSenha Then
                    fUsuario = fListaUsuarios.Item(index)
                End If
            Next
        Catch ex As Exception
        Finally
            Me.conection.Close()
        End Try
        Return fUsuario
    End Function

    Public Function CadastroTarefa(ByVal parTarefa As SuporteModel.Tarefa) As Boolean Implements IUsuario.CadastroTarefa

    End Function

    Public Function ExcluirTarefa(ByVal parIdTarefa As Integer) As Boolean Implements IUsuario.ExcluirTarefa

    End Function

    Public Function ListaTarefa(ByVal parDataInicio As Date, ByVal parDataFim As Date) As System.Collections.Generic.List(Of SuporteModel.Tarefa) Implements IUsuario.ListaTarefa

    End Function

    Public Function ListaTarefa(ByVal parIdUsuario As Integer) As System.Collections.Generic.List(Of SuporteModel.Tarefa) Implements IUsuario.ListaTarefa

    End Function

    Public Function ListaTarefa1(ByVal parDataInicio As Date, ByVal parDataFim As Date, ByVal parIdChamado As Integer) As System.Collections.Generic.List(Of SuporteModel.Tarefa) Implements IUsuario.ListaTarefa

    End Function

    Public Function AtualizarTecnico(ByVal parIdTecnico As Integer, ByVal parNovoTecnico As SuporteModel.Tecnico) As Boolean Implements IUsuario.AtualizarTecnico

    End Function

    Public Function ExcluirTecnico(ByVal parIdTecnoco As Integer) As Boolean Implements IUsuario.ExcluirTecnico
        Try
            Me.conection.Open()
        Catch ex As Exception

        Finally
            Me.conection.Close()
        End Try

    End Function

    Public Function ListarTecnicos() As System.Collections.Generic.List(Of SuporteModel.Tecnico) Implements IUsuario.ListarTecnicos
        Dim listaTecnicos As New List(Of Tecnico)
        Try
            Me.conection.Open()
            Dim comm As New SqlCommand("SELECT TEC_CODIGO,TEC_NOME,TEC_EMAIL,TEC_ATIVO,TEC_CELULAR,TEC_NEXTEL,TEC_RESIDENCIA FROM TECNICOS", Me.conection)
            Dim dataReader As SqlDataReader = comm.ExecuteReader()
            While dataReader.Read()
                Dim tecnico As New Tecnico
                tecnico.Ativo = Convert.ToBoolean(dataReader("TEC_ATIVO"))
                tecnico.Celular = dataReader("TEC_CELULAR").ToString()
                tecnico.Email = dataReader("TEC_EMAIL").ToString()
                tecnico.Nextel = dataReader("TEC_NEXTEL").ToString()
                tecnico.TelResidencial = dataReader("TEC_RESIDENCIA").ToString()
                tecnico.IdTecnico = dataReader("TEC_CODIGO").ToString()
                tecnico.Nome = dataReader("TEC_NOME").ToString()
                listaTecnicos.Add(tecnico)
            End While
        Catch ex As Exception

        Finally
            Me.conection.Close()
        End Try
        Return listaTecnicos
    End Function


End Class
